<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @author  Nguyen Xuan Hung
 * @email   hungnguyen@vietnambiz.com
 * @date    19.11.2010
 */
 
class Auth_model extends CI_Model {
    
    function __construct()
    {
        parent::__construct();
    }
    
    function login($user = '', $pass = '')
    {
        $user = $this->db->where('username', $user)
                         ->where('password', $pass)
                         //->where('status', 'active')
                         ->limit(1)->get('users')->row_array();

        if(isset($user['user_id']) && $user['user_id'] > 0) {
            $_SESSION['_admin'] = $user;
            return true;
        }
        else return false;
    }
    
    function logout()
    {
        //session_destroy();
        unset($_SESSION['_admin']);
        return true;
    }
    
    function check_permission($controller = '', $method = '')
    {
        $allow   = true;
        /*
        $user    = $_SESSION['_admin'];
        if($user['group_id'] == 1) $allow = true;
        $all_per = $this->db->from('modules as m')->join('permissions as p','m.id=p.module_id')->where('group_id', $user['group_id'])
                            ->where_in('m.name', array('*', $controller))
                            ->where_in('method', array('*', $method))
                            ->get()
                            ->result_array();

        if(count($all_per) < 1) {
            return false;
        }
        
        //print_r($all_per);
        
        foreach($all_per as $per) {
            if($per['status'] == 'active') $allow = true;
            else {$allow = false;break;}
        }*/

        return $allow;
    }
}
